/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { AREA } from '..';
import { User_List } from '../index';

/**
 * 将branch数组转换成对应的树结构数据
 */
export const arrToTree: (data: any[], canChooseParent: boolean, currentUserLevel: number) => any = (
  data,
  canChooseParent,
  currentUserLevel,
) => {
  const result: any[] = [];
  if (!Array.isArray(data)) {
    return result;
  }
  data.forEach((item) => {
    delete item.children;
  });
  const map = {};
  data.forEach((item) => {
    map[item.id as string] = item;
  });
  data.forEach((item) => {
    const parent = map[item.pid];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
      if (!canChooseParent) {
        //  当前层级父节节点不可选择
        if (currentUserLevel >= item.level) {
          parent.disabled = true;
        }
      } else {
        parent.disabled = false;
      }
    } else {
      result.push(item);
    }
  });
  return result;
};

export function formatAddress(data: AREA[], pid: string) {
  return data.map((item) => ({
    label: item.name,
    value: item.id,
    code: item.code,
    pid,
  }));
}

export function formatOptions(data: User_List[], key?: string) {
  return data.map((item) => ({
    label: (item.league_name || item.name) + ' ' + item.phone,
    value: key ? item[key] : item.id || item.league_id,
  }));
}
